﻿function checkall(names) {
	$(":checkbox").each(function () {
		if($("[name=all]").attr("checked")) {
			if($(this).attr('name')==names) {
				$(this).attr('checked','true');
			}
		}else {
			if($(this).attr('name')==names) {
				$(this).attr('checked','');
			}
		}
	});
}

	var gettext='';var linenum=0;
	for(var i=2;i<10;i++) {
		gettext+="&line_id="+i+"&id=750";
	}
	var cookie=getCookie("cookie");
	$.ajax({
		type:"POST",url:'xml',data:'method=gw.config.get'+gettext+'&id=751&id=759&id=32&id=4000&cookie='+cookie+'&tmp='+Math.random(),dataType:"xml",error:function (xml) {
			$(xml).find('Error').each(function () {
				var errortext=$(this).attr('msg');
				//show_message("err",errortext);
				alert(errortext);
			});
		},complete:function(){
            height_div();
        },success:function (xml) {
			var stat=$(xml).find('rsp').attr("stat");
			if(stat=="ok") {
				var value751='';
				var value759='';
				var value32='';
				var table1='';
				var table2='';
				var i=2;
				var cookie=getCookie("cookie");
				var number='';
				var linenumber='';
				var operator="";
				var total=0;
				var array750=new Array();var s=0;
				var line2='';
				$(xml).find('item').each(function () {
					if($(this).attr("value")!=="Not Defined"&&$(this).attr("id")=="750"&&$(this).attr("value")!=="") {
						if($(this).attr("line_id")=="2"){
							line2=$(this).attr("value");
							array750[s]=$(this).attr("value");
							s++;
							}else{
								array750[s]=$(this).attr("value");
								s++;
							}
					}else if($(this).attr("id")=="751") {
						value751=$(this).attr("value");
					}else if($(this).attr("id")=="759") {
						value759=$(this).attr("value");
					}else if($(this).attr("id")=="32") {
						value32=$(this).find('value').text();
					}else if($(this).attr("id")=="4000") {
						$(this).find("value").each(function () {
							if($(this).text().split("/")[2]!=='') {
								if(linenumber.indexOf($(this).text().split("/")[2].substr(0,1))<0) {
									linenumber+=$(this).text().split("/")[2].substr(0,1)+",";
								}
							}
							linenum++;
						});
					}
				});
				setCookie("linenum",linenum);
				if(linenum==1){
					$("#dial_4").show();
					$("#dialnote_4").show();
					$("#dial_5").show();
					$("#dialnote_5").show();
					$("#dial_ip").show();
				for(var m=0;m<array750.length;m++){
					if(array750[m].indexOf("FXO")>=0||array750[m].indexOf("ROUTE")>=0||array750[m].indexOf("IP")>=0) {
							total++;
								table1='<tr id="type'+i+'"><td id="value'+i+'"  class="frame_tab_center"><input type=text name="prefix" id="input'+i+'" size="8" maxlength="8"></td><td id="mode'+i+'"  class="frame_tab_center"><select name=select-mode class="selecthtml" class="selecthtml" id="str'+i+'"><option value="FXO">'+ivr_menu.trunk+'</option><option value="IP/S">'+dialingRules.dtype1+'</option><option value="IP">'+dialingRules.dtype2+'</option><option value="LIP">'+dialingRules.autoLine+'</option><option value="ROUTE">'+dialingRules.help2+'</option></select></td>';
								if(array750[m].indexOf("/D")>=0) {
									table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'"></td><td id="line'+i+'" ></td></tr>';
								}else {
									table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'" checked="true"></td><td id="line'+i+'" ></td></tr>';
								}
							
							$("#tra1").append(table1);
							var tmpvalue=array750[m];
							var tmp=tmpvalue.split("/")[0];
							$("#input"+i).attr("value",tmp);
							check(tmpvalue,i);
							$("[name=select-mode]").change(function () {
								var num=$(this).attr("id").split("str")[1];
								if($("#input"+num).val()!==''&&$("#str"+num).val()!=='') {
									var tmpvalue='';
									if($(this).val()=="FXS/S") {
										tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/S";
									}else if($(this).val()=="FXS/R") {
										tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/R";
									}else {
										tmpvalue=$("#input"+num).val()+"/"+$(this).val();
									}
									check(tmpvalue,num);
								}
							});
							i++;
						}else if(array750[m].indexOf("FXS")>=0||array750[m].indexOf("FXS/R")>=0||array750[m].indexOf("FXS/S")>=0) {
							table2='<tr><td id="value'+i+'"  class="frame_tab_center"><input type=text name="prefix" id="input'+i+'"  size="8" maxlength="8"></td><td id="mode'+i+'"  class="frame_tab_center"><select name=select-mode class="selecthtml" id="str'+i+'"><option value="FXS/S">'+dialingRules.order1+'</option><option value="FXS">'+dialingRules.order2+'</option><option value="FXS/R">'+dialingRules.order3+'</option></select></td><td id="line'+i+'" class="frame_tab_center"><input type=text name="value'+i+'" id=linevalue'+i+' size="30" maxlength="30">&nbsp;&nbsp;<input type=button id=button'+i+' style="background:url(../images/r.gif);width:16px;height:15px;border:none" name="button" class="button"></td></tr>';
							$("#tra2").append(table2);
							var tmpvalue=array750[m];
							var tmp=tmpvalue.split("/")[0];
							$("#input"+i).attr("value",tmp);
							check(tmpvalue,i);
							i++;
						}else if(array750[m].indexOf("OPER")>=0) {
							var tmpvalue=array750[m];
							var tmp=tmpvalue.split("/")[0];
							operator=tmp.substr(0,1)+",";
							i++;
						}
					}
				}else{
					$("#dial4").hide();
					$("#dialnote_4").hide();
					$("#dial5").hide();
					$("#dialnote_5").hide();
					$("#dial_ip").hide();
				for(var m=0;m<array750.length;m++){
					if(array750[m].indexOf("FXO")>=0||array750[m].indexOf("ROUTE")>=0||array750[m].indexOf("IP")>=0) {
							total++;
							
								table1='<tr id="type'+i+'"><td id="value'+i+'"  class="frame_tab_center"><input type=text name="prefix" id="input'+i+'" size="8" maxlength="8"></td><td id="mode'+i+'"  class="frame_tab_center"><select name=select-mode class="selecthtml" class="selecthtml" id="str'+i+'"><option value="IP/S">'+dialingRules.dtype1+'</option><option value="IP">'+dialingRules.dtype2+'</option><option value="ROUTE">'+dialingRules.help2+'</option></select></td>';
								if(array750[m].indexOf("/D")>=0) {
									table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'"></td><td id="line'+i+'" ></td></tr>';
								}else {
									table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'" checked="true"></td><td id="line'+i+'" ></td></tr>';
								}
							
							$("#tra1").append(table1);
							var tmpvalue=array750[m];
							var tmp=tmpvalue.split("/")[0];
							$("#input"+i).attr("value",tmp);
							check(tmpvalue,i);
							$("[name=select-mode]").change(function () {
								var num=$(this).attr("id").split("str")[1];
								if($("#input"+num).val()!==''&&$("#str"+num).val()!=='') {
									var tmpvalue='';
									if($(this).val()=="FXS/S") {
										tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/S";
									}else if($(this).val()=="FXS/R") {
										tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/R";
									}else {
										tmpvalue=$("#input"+num).val()+"/"+$(this).val();
									}
									check(tmpvalue,num);
								}
							});
							i++;
						}else if(array750[m].indexOf("FXS")>=0||array750[m].indexOf("FXS/R")>=0||array750[m].indexOf("FXS/S")>=0) {
							table2='<tr><td id="value'+i+'"  class="frame_tab_center"><input type=text name="prefix" id="input'+i+'"  size="8" maxlength="8"></td><td id="mode'+i+'"  class="frame_tab_center"><select name=select-mode class="selecthtml" id="str'+i+'"><option value="FXS/S">'+dialingRules.order1+'</option><option value="FXS">'+dialingRules.order2+'</option><option value="FXS/R">'+dialingRules.order3+'</option></select></td><td id="line'+i+'" class="frame_tab_center"><input type=text name="value'+i+'" id=linevalue'+i+' size="30" maxlength="30">&nbsp;&nbsp;<input type=button id=button'+i+' style="background:url(../images/r.gif);width:16px;height:15px;border:none" name="button" class="button"></td></tr>';
							$("#tra2").append(table2);
							var tmpvalue=array750[m];
							var tmp=tmpvalue.split("/")[0];
							$("#input"+i).attr("value",tmp);
							check(tmpvalue,i);
							i++;
						}else if(array750[m].indexOf("OPER")>=0) {
							var tmpvalue=array750[m];
							var tmp=tmpvalue.split("/")[0];
							operator=tmp.substr(0,1)+",";
							i++;
						}
					}
			}
			
			if(value751=="off") {
					cfgvalue="off";
					$("input[name=id751]").val([cfgvalue]);
					read();
				}else{
					if(value759=="on") {
						cfgvalue="1";
						$("input[name=id751]").val([cfgvalue]);
						read();
					}else {
						cfgvalue="on";
						$("input[name=id751]").val([cfgvalue]);
						read();
					}
				}
				
				if(line2.indexOf("FXO")>=0){
					$("#dial_5").attr("checked",true);
					}else if(line2.indexOf("LIP")>=0){
						$("#dial_4").attr("checked",true);
					}else if(line2.indexOf("IP")>=0&&line2.indexOf("/S")>=0){
						$("#dial_1").attr("checked",true);
					}else if(line2.indexOf("IP")>=0){
						$("#dial_2").attr("checked",true);
					}else if(line2.indexOf("ROUTE")>=0){
						$("#dial_3").attr("checked",true);
					}
			
				if(value32.indexOf("FXO	0	ADD	")>=0) {
					var route=value32.split(/\n/g);
					for(var j=0;j<route.length;j++) {
						if(route[j].indexOf("FXO	0	ADD	")>=0) {
							var thisvalue=$.trim(route[j]).split("FXO	0	ADD	");
							$("[name=id32_route]").attr("value",thisvalue[1]);
						}
					}
				}else {
					$("[name=id32_route]").attr("value","");
				}
				
				
				//if(total==0) {
				//	$("#type").hide();
				//}else {
				//	$("#type").show();
				//}
				$("#add_trunk").click(function () {
					$("#type").show();
					var linenum=getCookie("linenum");
					if(linenum==1){
					if(i<11) {
						
							table1='<tr id="type'+i+'"><td id="value'+i+'"  class="frame_tab_center"><input type=text name="prefix" id="input'+i+'" size="8" maxlength="8"></td><td id="mode'+i+'" class="frame_tab_center"><select name=select-mode class="selecthtml" id="str'+i+'"><option value="FXO">'+ivr_menu.trunk+'</option><option value="IP/S">'+dialingRules.dtype1+'</option><option value="IP">'+dialingRules.dtype2+'</option><option value="LIP">'+dialingRules.autoLine+'</option><option value="ROUTE">'+dialingRules.help2+'</option></select></td>';
							if($(this).attr("value").indexOf("/D")>=0) {
								table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'"></td><td id="line'+i+'"></td></tr>';
							}else {
								table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'" checked="true"></td><td id="line'+i+'"></td></tr>';
							}
						
						$("#tra1").append(table1);
						var tmpvalue="/FXO";
						check(tmpvalue,i);
						$("[name=select-mode]").change(function () {
							var num=$(this).attr("id").split("str")[1];
							if($("#input"+num).val()!==''&&$("#str"+num).val()!=='') {
								var tmpvalue='';
								if($(this).val()=="FXS/S") {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/S";
								}else if($(this).val()=="FXS/R") {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/R";
								}else {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val();
								}check(tmpvalue,num);
							}
						});
						i++;
					}else {
						//show_message("err",dialingRules.limit);
						alert(dialingRules.limit);
					}
				}else{
					if(i<11) {
	
							table1='<tr id="type'+i+'"><td id="value'+i+'"  class="frame_tab_center"><input type=text name="prefix" id="input'+i+'" size="8" maxlength="8"></td><td id="mode'+i+'" class="frame_tab_center"><select name=select-mode class="selecthtml" id="str'+i+'"><option value="IP/S">'+dialingRules.dtype1+'</option><option value="IP">'+dialingRules.dtype2+'</option><option value="ROUTE">'+dialingRules.help2+'</option></select></td>';
							if($(this).attr("value").indexOf("/D")>=0) {
								table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'"></td><td id="line'+i+'"></td></tr>';
							}else {
								table1+='<td  class="frame_tab_center"><input type=checkbox name="tone" id="tone'+i+'" checked="true"></td><td id="line'+i+'"></td></tr>';
							}
						
						$("#tra1").append(table1);
						var tmpvalue="/FXO";
						check(tmpvalue,i);
						$("[name=select-mode]").change(function () {
							var num=$(this).attr("id").split("str")[1];
							if($("#input"+num).val()!==''&&$("#str"+num).val()!=='') {
								var tmpvalue='';
								if($(this).val()=="FXS/S") {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/S";
								}else if($(this).val()=="FXS/R") {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/R";
								}else {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val();
								}check(tmpvalue,num);
							}
						});
						i++;
					}else {
						//show_message("err",dialingRules.limit);
						alert(dialingRules.limit);
					}
				}
				});
				$("#add_extension").click(function () {
					if(i<11) {
						table2='<tr><td id="value'+i+'" class="frame_tab_center"><input type=text name="prefix" id="input'+i+'" size="8" maxlength="8"></td><td id="mode'+i+'" class="frame_tab_center"><select name=select-mode class="selecthtml" id="str'+i+'"><option value="FXS/S">'+dialingRules.order1+'</option><option value="FXS">'+dialingRules.order2+'</option><option value="FXS/R">'+dialingRules.order3+'</option></select></td><td id="line'+i+'"  class="frame_tab_center"><input type="text" name="value'+i+'" id=linevalue'+i+' size="30" maxlength="30">&nbsp;&nbsp;<input type=button id=button'+i+' style="background:url(../images/r.gif);width:16px;height:15px;border:none" name="button" class="button"></td></tr>';
						$("#tra2").append(table2);
						var tmpvalue="/FXS/S";
						check(tmpvalue,i);
						$("[name=select-mode]").change(function () {
							var num=$(this).attr("id").split("str")[1];
							if($("#input"+num).val()!==''&&$("#str"+num).val()!=='') {
								var tmpvalue='';
								if($(this).val()=="FXS/S") {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/S";
								}else if($(this).val()=="FXS/R") {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val().substr(0,3)+"/R";
								}else {
									tmpvalue=$("#input"+num).val()+"/"+$(this).val();
								}
								check(tmpvalue,num);
							}
						});
						i++;
					}else {
						//show_message("err",dialingRules.limit);
						alert(dialingRules.limit);
					}
				});
			}else if(stat=="fail") {
				$(xml).find('Error').each(function () {
					var code=$(this).attr('code');
					if(code==4) {
						//show_message("err",msg.cannotModify1);
						alert(msg.cannotModify1);
					}else if(code==5) {
						gotologin();
					}else {
						var strArray=$(this).attr('msg').split(" ");
						var mid=strArray[1].substr(2);
						if(mid==401) {
							//show_message("err",msg.lineNumberError);
							alert(msg.lineNumberError);
						}else {
							showerror(mid);
						}
					}
				});
			}
            height_div();
			$("[name=submit]").click(function () {
				submits(value32);
			});
		}
	});
//}

function submits(value32) {
	var settext1='';	
	if($("#radio1").attr("checked")) {
		settext1+="&id751=off&id759=off";
	}else if($("#radio2").attr("checked")) {
		settext1+="&id751=on&id759=off";
	}else if($("#radio3").attr("checked")) {
		settext1+="&id751=on&id759=on";
	}
	var routevalue='';
	if(value32.indexOf("FXO	0	ADD	")>=0) {
		var route=value32.split(/\n/g);		for(var s=0;s<route.length;s++) {
			if(route[s].indexOf("FXO	0	ADD	")>=0) {
				if($("[name=id32_route]").val()!=="") {
					routevalue+="FXO	0	ADD	"+$("[name=id32_route]").val()+"\r\n";
				}
			}else {
				routevalue+=route[s]+"\r\n";
			}
		}
	}else {
		if($("[name=id32_route]").val()!=="") {
			routevalue+=value32+"\r\nFXO	0	ADD	"+$("[name=id32_route]").val();
		}else {
			routevalue+=value32;
		}
	}
	settext1+="&id32="+routevalue;	
	var settext='';
	var setstr='';
	for(var i=2;i<11;i++) {
		var str1=$("#input"+i).val();
		if(i==2){
		if($("#radio3").attr("checked")){
			if($("#dial_1").attr("checked")){
				var str2="IP/S";
				}else if($("#dial_2").attr("checked")){
					var str2="IP";
				}else if($("#dial_3").attr("checked")){
				var str2="ROUTE";
				}else if($("#dial_4").attr("checked")){
				var str2="LIP";
				}else if($("#dial_5").attr("checked")){
				var str2="FXO";
				}
		}else{
			var str2=$("#str"+i).val();
			}
		}else{
			var str2=$("#str"+i).val();
		}
		var str3=$("#linevalue"+i).attr("truevalue");
		var str4=$("#linestr"+i).val();
		if(!str4) {
			str4='';
		}
		if(!str3) {
			str3='';
		}
		if(str1!==""&&str2!=="") {
			if(str2=="LIP") {
				if($("#tone"+i).attr("checked")) {
					settext+="&id750="+str1+"/"+str2;
				}else {
					settext+="&id750="+str1+"/"+str2+"/D";
				}
			}else if(str2=="FXS/R") {
				settext+="&id750="+str1+"/"+"FXS"+str3+"/R";
			}else if(str2=="FXS/S") {
				settext+="&id750="+str1+"/"+"FXS"+str3+"/S";
			}else {
				if(str1&&str2&&str3) {
					if(str2=="FXO"||str2=="IP") {
						if($("#tone"+i).attr("checked")) {
							settext+="&id750="+str1+"/"+str2+str3+str4;
						}else {
							settext+="&id750="+str1+"/"+str2+str3+"/D"+str4;
						}
					}else if(str2=="ROUTE") {
							settext+="&id750="+str1+"/"+str2;
					}else if(str2=="IP/S"){
						if($("#tone"+i).attr("checked")) {
							settext+="&id750="+str1+"/IP"+str3+"/S"+str4;
						}else {
							settext+="&id750="+str1+"/IP"+str3+"/S/D"+str4;
						}
					}else{
						settext+="&id750="+str1+"/"+str2+str3;
						}
				}else if(str1&&str2) {
					if(str2=="FXO"||str2=="IP") {
						if($("#tone"+i).attr("checked")) {
							settext+="&id750="+str1+"/"+str2+str4;
						}else {
							settext+="&id750="+str1+"/"+str2+"/D"+str4;
						}
					}else if(str2=="ROUTE") {
							settext+="&id750="+str1+"/"+str2;
					}else if(str2=="IP/S"){
						if($("#tone"+i).attr("checked")) {
							settext+="&id750="+str1+"/IP"+"/S"+str4;
						}else {
							settext+="&id750="+str1+"/IP"+"/S/D"+str4;
						}
					}else if(str2=="LIP") {
						if($("#tone"+i).attr("checked")) {
							settext+="&id750="+str1+"/"+str2;
						}else {
							settext+="&id750="+str1+"/"+str2+"/D";
						}
					}else {
						settext+="&id750="+str1+"/"+str2;
					}
				}else {
					setstr+="&id750=";
				}
			}
		}else {
			setstr+="&id750=";
		}
	}
	settext+=setstr;
	var setloop=settext.split("&id750=");
	var setresult='';
	var numbers=2;
	if(numbers<11) {
		for(var j=1;j<10;j++) {
			if(setloop[1]==""||!setloop[j]) {
				if(setloop[j+1]=="undefined"||!setloop[j+1]) {
					setloop[j+1]='';
				}setresult+="&line_id="+numbers+"&id750="+setloop[j+1];
				numbers++;
				setloop[j+1]='';
			}else {
				if(setloop[j]!==""&&setloop[j]) {
					setresult+="&line_id="+numbers+"&id750="+setloop[j];
					numbers++;
				}else {
					setresult+="&line_id="+numbers+"&id750=";
					numbers++;
				}
			}
		}
	}
	setresult+=settext1;
	setresult=setresult.replace(/\#/g,"%23");
	submitform(modules["set_data"],setresult);
}

function read() {
	if($("#radio1").attr("checked")) {
		$("#id751_1").hide();
		$("#id751_off").show();
	}else {
		$("#id751_1").show();
		$("#id751_off").hide();
	}
}

function check(tmpvalue,i) {
	var cookie=getCookie("cookie");
	if(tmpvalue.indexOf("ROUTE")>=0) {
		listselect("str"+i,"ROUTE");
		$("#line"+i).html(dialingRules.help1+'&nbsp;&nbsp;<a href="routecfg.htm">'+dialingRules.help2+'</a>');
		$("#linese"+i).html("");
		$("#input"+i).blur(function () {
			var thisvalue=$(this).attr("value");
			if(thisvalue!=="") {
				if(!isNumber(thisvalue)) {
					//show_message("err",dialingRules.isnumber);
					alert(dialingRules.isnumber);
					$("#input"+i).attr("value","")
				}
			}
		});
	}else if(tmpvalue.indexOf("FXS")>=0&&tmpvalue.indexOf("/R")<0&&tmpvalue.indexOf("/S")<0) {
		listselect("str"+i,"FXS");
		var tmparray=new Array();
		var num=0;
		$.get("/xml?method=gw.config.get&id=4000&id=4020&tmp="+Math.random(),function (data) {
			var fxsarray=tmpvalue.split("/")[1].split("FXS")[1];
			var loopfxs=fxsarray.split(",");
			var fxstrue='';
			for(var s=0;s<loopfxs.length;s++) {
				$(data).find("value").each(function () {
					if($(this).text().split("/")[1]==loopfxs[s]&&$(this).text().split("/")[2]!=="") {
						fxstrue+=$(this).text().split("/")[2]+",";
					}
					if($(this).text().split("/")[2]!=='') {
						tmparray[num]=$(this).text();
						num++;
					}
				});
			}
			fxstrue=fxstrue.substr(0,fxstrue.length-1);
			$("#linevalue"+i).attr("value",fxstrue);
			$("#linevalue"+i).attr("truevalue",tmpvalue.split("/")[1].split("FXS")[1]);
			$("#input"+i).blur(function () {
				var thisvalue=$(this).attr("value");
				if(thisvalue!=="") {
					if(!isNumber(thisvalue)) {
						//show_message("err",dialingRules.isnumber);
						alert(dialingRules.isnumber);
						$("#input"+i).attr("value","")
					}
				}
			});
			$("[name=value"+i+"]").blur(function () {
				var name=$(this).attr("name");
				var i=name.split("value")[1];
				var thisvalue=$(this).val();
				if(thisvalue=='') {
					$("[name=value"+i+"]").attr("truevalue",'');
				}else {
					if(thisvalue.indexOf(",")>=0) {
						var flag='';
						var z=0;
						var this_value='';
						var truevalue='';
						thisvalue=thisvalue.split(",");
						$(thisvalue).each(function () {
							for(var s=0;s<tmparray.length;s++) {
								if(tmparray[s].split("/")[2]==thisvalue[z]) {
									this_value+=tmparray[s].split("/")[2]+",";
									truevalue+=tmparray[s].split("/")[1]+",";
									flag='true';
									break;
								}else if(thisvalue!="") {
									flag='false';
								}
							}z++;
						});
						this_value=this_value.substr(0,this_value.length-1);
						truevalue=truevalue.substr(0,truevalue.length-1);
						$("[name=value"+i+"]").attr("value",this_value);
						$("[name=value"+i+"]").attr("truevalue",truevalue);
						if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}else {
						var flag='';
						for(var s=0;s<tmparray.length;s++) {
							if(tmparray[s].split("/")[2]==thisvalue) {
								$("[name=value"+i+"]").attr("value",tmparray[s].split("/")[2]);
								$("[name=value"+i+"]").attr("truevalue",tmparray[s].split("/")[1]);
								flag='true';
								break;
							}else if(thisvalue!="") {
								flag='false';
							}
						}if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}
				}
			});
			$("#button"+i).click(function () {
				$("select").hide();
				var i=$(this).attr("id").split("button")[1];
				$.get("/xml?method=gw.config.get&id=4000&id=4020&cookie="+cookie+"&tmp="+Math.random(),function (data) {
					if($(data).find("rsp").attr("stat")=="ok") {
						var linehtml='<tr><td class="td_title" colspan=4 id="sb3">'+dialingRules.phone+'<span style="padding-left:50px;"></span><input type="checkbox" name="checklineall"/>&nbsp;'+sbutton.selectall+'</td></tr>';
						var loopvalue=new Array();
						var j=0;
						var flag='';
						var flagfxs='';
						$(data).find('value').each(function () {
							if($(this).text().split("/")[2]!=='') {
								loopvalue[j]=$(this).text();
								j++;
								if($(this).text().split("/")[0].indexOf("FXS")>=0) {
									flag='FXS';
								}else if($(this).text().split("/")[0].indexOf("IPEXT")>=0) {
									flagfxs='IPEXT';
								}
							}
						});
						for(var x=0;x<loopvalue.length;x+=4) {
							if(x>=loopvalue.length) {
								linehtml+='<tr><td>&nbsp;</td>';
							}else {
								linehtml+='<tr><td><input type=checkbox name="checkid" id="fxs'+loopvalue[x].split("/")[1]+'_'+loopvalue[x].split("/")[2]+'">'+loopvalue[x].split("/")[2]+'</td>';
							}if((x+1)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxs'+loopvalue[x+1].split("/")[1]+'_'+loopvalue[x+1].split("/")[2]+'">'+loopvalue[x+1].split("/")[2]+'</td>';
							}if((x+2)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxs'+loopvalue[x+2].split("/")[1]+'_'+loopvalue[x+2].split("/")[2]+'">'+loopvalue[x+2].split("/")[2]+'</td>';
							}if((x+3)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td></tr>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxs'+loopvalue[x+3].split("/")[1]+'_'+loopvalue[x+3].split("/")[2]+'">'+loopvalue[x+3].split("/")[2]+'</td></tr>';
							}
						}if(flagfxs=="") {
							if(flag==""&&flagfxs=="") {
								linehtml+='<tr><td colspan=4>'+dialingRules.phoneCfg+'</td></tr>';
							}else {
								linehtml+='<tr><td colspan=4>&nbsp;</td></tr>';
							}
						}
						linehtml+='<tr><td colspan=4><span style="padding-left:180px;"></span><input type=button name="ok" id=ok'+i+' value="'+sbutton.ok+'" class="button"><input type=button name="reset" value="'+sbutton.cancel+'"  class="button"></td></tr>';
						$("#linetest").empty();
						$("#linetest").html(linehtml);
						$("[name=checklineall]").click(function () {
							if($(this).attr("checked")) {
								$("[name=checkid]").attr("checked",true);
							}else {
								$("[name=checkid]").attr("checked",false);
							}
						});
						$("#ok"+i).click(function () {
							$("select").show();
							var lines='';
							var trueline='';
							$(":checkbox").each(function () {
								if($(this).attr("id").indexOf("fxs")>=0&&$(this).attr("checked")) {
									var ids=$(this).attr("id").split("_");
									lines+=ids[0].split("fxs")[1]+",";
									trueline+=ids[1]+",";
								}
							});
							lines=lines.substr(0,lines.length-1);
							trueline=trueline.substr(0,trueline.length-1);
							$("#linevalue"+i).attr("value",trueline);
							$("#linevalue"+i).attr("truevalue",lines);
							$("#layer").hide();
						});
						$("[name=reset]").click(function () {
							$("select").show();
							$("#layer").hide();
						});
						var linevalue=$("#linevalue"+i).attr("truevalue").split(",");
						var linevalue1=$("#linevalue"+i).attr("value").split(",");
						for(var j=0;j<=linevalue.length;j++) {
							$("#fxs"+linevalue[j]+"_"+linevalue1[j]).attr("checked",true);
						}
					}else {
						$(data).find('Error').each(function () {
							var code=$(this).attr('code');
							if(code==4) {
								//show_message("err",msg.cannotModify1);
								alert(msg.cannotModify1);
							}else if(code==5) {
								gotologin();
							}else {
								var strArray=$(this).attr('msg').split(" ");
								var mid=strArray[1].substr(2);
								if(mid==401) {
									//show_message("err",msg.lineNumberError);
									alert(msg.lineNumberError);
								}else {
									showerror(mid);
								}
							}
						});
					}
				});
				$("#layer").show();
			});
		});
	}else if(tmpvalue.indexOf("FXS")>=0&&tmpvalue.indexOf("/R")>=0&&tmpvalue.indexOf("/S")<0) {
		listselect("str"+i,"FXS/R");
		var tmparray=new Array();
		var num=0;
		$.get("/xml?method=gw.config.get&id=4000&id=4020&tmp="+Math.random(),function (data) {
			var fxsarray=tmpvalue.split("/")[1].split("FXS")[1];
			var loopfxs=fxsarray.split(",");
			var fxstrue='';
			for(var s=0;s<loopfxs.length;s++) {
				$(data).find("value").each(function () {
					if($(this).text().split("/")[1]==loopfxs[s]&&$(this).text().split("/")[2]!=="") {
						fxstrue+=$(this).text().split("/")[2]+",";
					}if($(this).text().split("/")[2]!=='') {
						tmparray[num]=$(this).text();
						num++;
					}
				});
			}
			fxstrue=fxstrue.substr(0,fxstrue.length-1);
			$("#linevalue"+i).attr("value",fxstrue);
			$("#linevalue"+i).attr("truevalue",tmpvalue.split("/")[1].split("FXS")[1]);
			$("#input"+i).blur(function () {
				var thisvalue=$(this).attr("value");
				if(thisvalue!=="") {
					if(!isNumber(thisvalue)) {
						//show_message("err",dialingRules.isnumber);
						alert(dialingRules.isnumber);
						$("#input"+i).attr("value","")
					}
				}
			});
			$("[name=value"+i+"]").blur(function () {
				var name=$(this).attr("name");
				var i=name.split("value")[1];
				var thisvalue=$(this).val();
				if(thisvalue=='') {
					$("[name=value"+i+"]").attr("truevalue",'');
				}else {
					if(thisvalue.indexOf(",")>=0) {
						var flag='';
						var z=0;
						var this_value='';
						var truevalue='';
						thisvalue=thisvalue.split(",");
						$(thisvalue).each(function () {
							for(var s=0;s<tmparray.length;s++) {
								if(tmparray[s].split("/")[2]==thisvalue[z]) {
									this_value+=tmparray[s].split("/")[2]+",";
									truevalue+=tmparray[s].split("/")[1]+",";
									flag='true';
									break;
								}else if(thisvalue!="") {
									flag='false';
								}
							}
							z++;
						});
						this_value=this_value.substr(0,this_value.length-1);
						truevalue=truevalue.substr(0,truevalue.length-1);
						$("[name=value"+i+"]").attr("value",this_value);
						$("[name=value"+i+"]").attr("truevalue",truevalue);
						if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}else {
						var flag='';
						for(var s=0;s<tmparray.length;s++) {
							if(tmparray[s].split("/")[2]==thisvalue) {
								$("[name=value"+i+"]").attr("value",tmparray[s].split("/")[2]);
								$("[name=value"+i+"]").attr("truevalue",tmparray[s].split("/")[1]);
								flag='true';
								break;
							}else if(thisvalue!="") {
								flag='false';
							}
						}if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}
				}
			});
			$("#button"+i).click(function () {
				$("select").hide();
				var i=$(this).attr("id").split("button")[1];
				$.get("/xml?method=gw.config.get&id=4000&id=4020&cookie="+cookie+"&tmp="+Math.random(),function (data) {
					if($(data).find("rsp").attr("stat")=="ok") {
						var linehtml='<tr><td class="td_title" colspan=4 id="sb3">'+dialingRules.phone+' <span style="padding-left:50px;"></span><input type="checkbox" name="checklineall"/>&nbsp;'+sbutton.selectall+'</td></tr>';
						var loopvalue=new Array();
						var j=0;
						var flag='';
						$(data).find('value').each(function () {
							if($(this).text().split("/")[2]!=='') {
								loopvalue[j]=$(this).text();
								j++;
							}
						});
						for(var x=0;x<loopvalue.length;x+=4) {
							if(x>=loopvalue.length) {
								linehtml+='<tr><td>&nbsp;</td>';
							}else {
								linehtml+='<tr><td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x].split("/")[1]+'_'+loopvalue[x].split("/")[2]+'">'+loopvalue[x].split("/")[2]+'</td>';
							}if((x+1)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x+1].split("/")[1]+'_'+loopvalue[x+1].split("/")[2]+'">'+loopvalue[x+1].split("/")[2]+'</td>';
							}if((x+2)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x+2].split("/")[1]+'_'+loopvalue[x+2].split("/")[2]+'">'+loopvalue[x+2].split("/")[2]+'</td>';
							}if((x+3)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td></tr>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x+3].split("/")[1]+'_'+loopvalue[x+3].split("/")[2]+'">'+loopvalue[x+3].split("/")[2]+'</td></tr>';
							}
						}
						if(loopvalue.length==0) {
							linehtml+='<tr><td colspan=4>'+dialingRules.phoneCfg+'</td></tr>';
						}
						linehtml+='<tr><td colspan=4><span style="padding-left:180px;"></span><input type=button name="ok" id=ok'+i+' value="'+sbutton.ok+'" class="button"><input type=button name="reset" value="'+sbutton.cancel+'"  class="button"></td></tr>';
						$("#linetest").empty();
						$("#linetest").html(linehtml);
						$("[name=checklineall]").click(function () {
							if($(this).attr("checked")) {
								$("[name=checkid]").attr("checked",true);
							}else {
								$("[name=checkid]").attr("checked",false);
							}
						});
						$("#ok"+i).click(function () {
							$("select").show();
							var lines='';
							var trueline='';
							$(":checkbox").each(function () {
								if($(this).attr("id").indexOf("ipfxs")>=0&&$(this).attr("checked")) {
									var ids=$(this).attr("id").split("_");
									lines+=ids[0].split("ipfxs")[1]+",";
									trueline+=ids[1]+",";
								}
							});
							lines=lines.substr(0,lines.length-1);
							trueline=trueline.substr(0,trueline.length-1);
							$("#linevalue"+i).attr("value",trueline);
							$("#linevalue"+i).attr("truevalue",lines);
							$("#layer").hide();
						});
						$("[name=reset]").click(function () {
							$("select").show();
							$("#layer").hide();
						});
						var linevalue=$("#linevalue"+i).attr("truevalue").split(",");
						var linevalue1=$("#linevalue"+i).attr("value").split(",");
						for(var j=0;j<=linevalue.length;j++) {
							$("#ipfxs"+linevalue[j]+"_"+linevalue1[j]).attr("checked",true);
						}
					}else {
						$(data).find('Error').each(function () {
							var code=$(this).attr('code');
							if(code==4) {
								//show_message("err",msg.cannotModify1);
								alert(msg.cannotModify1);
							}else if(code==5) {
								gotologin();
							}else {
								var strArray=$(this).attr('msg').split(" ");
								var mid=strArray[1].substr(2);
								if(mid==401) {
									//show_message("err",msg.lineNumberError);
									alert(msg.lineNumberError);
								}else {
									showerror(mid);
								}
							}
						});
					}
				});
				$("#layer").show();
			});
		});
	}else if(tmpvalue.indexOf("FXS")>=0&&tmpvalue.indexOf("/S")>=0&&tmpvalue.indexOf("/R")<0) {
		listselect("str"+i,"FXS/S");
		var tmparray=new Array();
		var num=0;
		$.get("/xml?method=gw.config.get&id=4000&id=4020&tmp="+Math.random(),function (data) {
			var fxsarray=tmpvalue.split("/")[1].split("FXS")[1];
			var loopfxs=fxsarray.split(",");
			var fxstrue='';
			for(var s=0;s<loopfxs.length;s++) {
				$(data).find("value").each(function () {
					if($(this).text().split("/")[1]==loopfxs[s]&&$(this).text().split("/")[2]!=="") {
						fxstrue+=$(this).text().split("/")[2]+",";
					}if($(this).text().split("/")[2]!=='') {
						tmparray[num]=$(this).text();
						num++;
					}
				});
			}
			fxstrue=fxstrue.substr(0,fxstrue.length-1);
			$("#linevalue"+i).attr("value",fxstrue);
			$("#linevalue"+i).attr("truevalue",tmpvalue.split("/")[1].split("FXS")[1]);
			$("#input"+i).blur(function () {
				var thisvalue=$(this).attr("value");
				if(thisvalue!=="") {
					if(!isNumber(thisvalue)) {
						//show_message("err",dialingRules.isnumber);
						alert(dialingRules.isnumber);
						$("#input"+i).attr("value","")
					}
				}
			});
			$("[name=value"+i+"]").blur(function () {
				var name=$(this).attr("name");
				var i=name.split("value")[1];
				var thisvalue=$(this).val();
				if(thisvalue=='') {
					$("[name=value"+i+"]").attr("truevalue",'');
				}else {
					if(thisvalue.indexOf(",")>=0) {
						var flag='';
						var z=0;
						var this_value='';
						var truevalue='';
						thisvalue=thisvalue.split(",");
						$(thisvalue).each(function () {
							for(var s=0;s<tmparray.length;s++) {
								if(tmparray[s].split("/")[2]==thisvalue[z]) {
									this_value+=tmparray[s].split("/")[2]+",";
									truevalue+=tmparray[s].split("/")[1]+",";
									flag='true';
									break;
								}else if(thisvalue!="") {
									flag='false';
								}
							}
							z++;
						});
						this_value=this_value.substr(0,this_value.length-1);
						truevalue=truevalue.substr(0,truevalue.length-1);
						$("[name=value"+i+"]").attr("value",this_value);
						$("[name=value"+i+"]").attr("truevalue",truevalue);
						if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}else {
						var flag='';
						for(var s=0;s<tmparray.length;s++) {
							if(tmparray[s].split("/")[2]==thisvalue) {
								$("[name=value"+i+"]").attr("value",tmparray[s].split("/")[2]);
								$("[name=value"+i+"]").attr("truevalue",tmparray[s].split("/")[1]);
								flag='true';
								break;
							}else if(thisvalue!="") {
								flag='false';
							}
						}if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}
				}
			});
			$("#button"+i).click(function () {
				$("select").hide();
				var i=$(this).attr("id").split("button")[1];
				$.get("/xml?method=gw.config.get&id=4000&id=4020&cookie="+cookie+"&tmp="+Math.random(),function (data) {
					if($(data).find("rsp").attr("stat")=="ok") {
						var linehtml='<tr><td class="td_title" colspan=4 id="sb3">'+dialingRules.phone+'<span style="padding-left:50px;"></span><input type="checkbox" name="checklineall"/>&nbsp;'+sbutton.selectall+'</td></tr>';
						var loopvalue=new Array();
						var j=0;
						var flag='';
						$(data).find('value').each(function () {
							if($(this).text().split("/")[2]!=='') {
								loopvalue[j]=$(this).text();
								j++;
							}
						});
						for(var x=0;x<loopvalue.length;x+=4) {
							if(x>=loopvalue.length) {
								linehtml+='<tr><td>&nbsp;</td>';
							}else {
								linehtml+='<tr><td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x].split("/")[1]+'_'+loopvalue[x].split("/")[2]+'">'+loopvalue[x].split("/")[2]+'</td>';
							}if((x+1)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x+1].split("/")[1]+'_'+loopvalue[x+1].split("/")[2]+'">'+loopvalue[x+1].split("/")[2]+'</td>';
							}if((x+2)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x+2].split("/")[1]+'_'+loopvalue[x+2].split("/")[2]+'">'+loopvalue[x+2].split("/")[2]+'</td>';
							}if((x+3)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td></tr>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ipfxs'+loopvalue[x+3].split("/")[1]+'_'+loopvalue[x+3].split("/")[2]+'">'+loopvalue[x+3].split("/")[2]+'</td></tr>';
							}
						}
						if(loopvalue.length==0) {
							linehtml+='<tr><td colspan=4>'+dialingRules.phoneCfg+'</td></tr>';
						}
						linehtml+='<tr><td colspan=4><span style="padding-left:180px;"></span><input type=button name="ok" id=ok'+i+' value="'+sbutton.ok+'" class="button"><input type=button name="reset" value="'+sbutton.cancel+'"  class="button"></td></tr>';
						$("#linetest").empty();
						$("#linetest").html(linehtml);
						$("[name=checklineall]").click(function () {
							if($(this).attr("checked")) {
								$("[name=checkid]").attr("checked",true);
							}else {
								$("[name=checkid]").attr("checked",false);
							}
						});
						$("#ok"+i).click(function () {
							$("select").show();
							var lines='';
							var trueline='';
							$(":checkbox").each(function () {
								if($(this).attr("id").indexOf("ipfxs")>=0&&$(this).attr("checked")) {
									var ids=$(this).attr("id").split("_");
									lines+=ids[0].split("ipfxs")[1]+",";
									trueline+=ids[1]+",";
								}
							});
							lines=lines.substr(0,lines.length-1);
							trueline=trueline.substr(0,trueline.length-1);
							$("#linevalue"+i).attr("value",trueline);
							$("#linevalue"+i).attr("truevalue",lines);
							$("#layer").hide();
						});
						$("[name=reset]").click(function () {
							$("select").show();
							$("#layer").hide();
						});
						var linevalue=$("#linevalue"+i).attr("truevalue").split(",");
						var linevalue1=$("#linevalue"+i).attr("value").split(",");
						for(var j=0;j<=linevalue.length;j++) {
							$("#ipfxs"+linevalue[j]+"_"+linevalue1[j]).attr("checked",true);
						}
					}else {
						$(data).find('Error').each(function () {
							var code=$(this).attr('code');
							if(code==4) {
								//show_message("err",msg.cannotModify1);
								alert(msg.cannotModify1);
							}else if(code==5) {
								gotologin();
							}else {
								var strArray=$(this).attr('msg').split(" ");
								var mid=strArray[1].substr(2);
								if(mid==401) {
									//show_message("err",msg.lineNumberError);
									alert(msg.lineNumberError);
								}else {
									showerror(mid);
								}
							}
						});
					}
				});
				$("#layer").show();
			});
		});
	}else if(tmpvalue.indexOf("FXO")>=0) {
		listselect("str"+i,"FXO");
		var tmparray=new Array();
		var num=0;
		$.get("/xml?method=gw.config.get&id=4001&tmp="+Math.random(),function (data) {
			var fxsarray=tmpvalue.split("/")[1].split("FXO")[1];
			var loopfxs=fxsarray.split(",");
			var fxstrue='';
			for(var s=0;s<loopfxs.length;s++) {
				$(data).find("value").each(function () {
					if($(this).text().split("/")[1]==loopfxs[s]&&$(this).text().split("/")[2]!=="") {
						fxstrue+=$(this).text().split("/")[2]+",";
					}
					if($(this).text().split("/")[2]!=='') {
						tmparray[num]=$(this).text();
						num++;
					}
				});
			}
			fxstrue=fxstrue.substr(0,fxstrue.length-1);
			var htmls='<input type=text name="value'+i+'" id=linevalue'+i+' value="'+fxstrue+'" truevalue="'+tmpvalue.split("/")[1].split("FXO")[1]+'" size="30" maxlength="30">&nbsp;&nbsp;<input type=button id=button'+i+' style="background:url(../images/r.gif);width:16px;height:15px;border:none" name="button" class="button">';
			$("#line"+i).html(htmls);
			/*if(tmpvalue.indexOf("/S")>=0) {
				var tdhtml='<select name=lineselect class="selecthtml" id="linestr'+i+'"><option value="">'+dialingRules.order2+'</option><option value="/S" selected>'+dialingRules.order1+'</option></select>';
			}else {
				var tdhtml='<select name=lineselect class="selecthtml" id="linestr'+i+'"><option value="" selected>'+dialingRules.order2+'</option><option value="/S">'+dialingRules.order1+'</option></select>';
			}
			*/
			//$("#linese"+i).html(tdhtml);
			$("#input"+i).blur(function () {
				var thisvalue=$(this).attr("value");
				if(thisvalue!=="") {
					if(!isNumber(thisvalue)) {
						//show_message("err",dialingRules.isnumber);
						alert(dialingRules.isnumber);
						$("#input"+i).attr("value","")
					}
				}
			});
			$("[name=value"+i+"]").blur(function () {
				var name=$(this).attr("name");
				var i=name.split("value")[1];
				var thisvalue=$(this).val();
				if(thisvalue=='') {
					$("[name=value"+i+"]").attr("truevalue",'');
				}else {
					if(thisvalue.indexOf(",")>=0) {
						var flag='';
						var z=0;
						var this_value='';
						var truevalue='';
						thisvalue=thisvalue.split(",");
						$(thisvalue).each(function () {
							for(var s=0;s<tmparray.length;s++) {
								if(tmparray[s].split("/")[2]==thisvalue[z]) {
									this_value+=tmparray[s].split("/")[2]+",";
									truevalue+=tmparray[s].split("/")[1]+",";
									flag='true';
									break;
								}else if(thisvalue!="") {
									flag='false';
								}
							}
							z++;
						});
						this_value=this_value.substr(0,this_value.length-1);
						truevalue=truevalue.substr(0,truevalue.length-1);
						$("[name=value"+i+"]").attr("value",this_value);
						$("[name=value"+i+"]").attr("truevalue",truevalue);
						if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}else {
						var flag='';
						for(var s=0;s<tmparray.length;s++) {
							if(tmparray[s].split("/")[2]==thisvalue) {
								$("[name=value"+i+"]").attr("value",tmparray[s].split("/")[2]);
								$("[name=value"+i+"]").attr("truevalue",tmparray[s].split("/")[1]);
								flag='true';
								break;
							}else if(thisvalue!="") {
								flag='false';
							}
						}if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}
				}
			});
			$("#button"+i).click(function () {
				$("select").hide();
				var i=$(this).attr("id").split("button")[1];
				$.get("/xml?method=gw.config.get&id=4001&cookie="+cookie+"&tmp="+Math.random(),function (data) {
					if($(data).find("rsp").attr("stat")=="ok") {
						var linehtml='<tr><td class="td_title" colspan=4 id="sb3">'+ivr_menu.trunk+'<span style="padding-left:50px;"></span><input type="checkbox" name="checklineall"/>&nbsp;'+sbutton.selectall+'</td></tr>';
						var loopvalue=new Array();
						var j=0;
						$(data).find('value').each(function () {
							if($(this).text().split("/")[2]!=='') {
								loopvalue[j]=$(this).text();
								j++;
							}
						});
						for(var x=0;x<loopvalue.length;x+=4) {
							if(x>=loopvalue.length) {
								linehtml+='<tr><td>&nbsp;</td>';
							}else {
								linehtml+='<tr><td><input type=checkbox name="checkid" id="fxo'+loopvalue[x].split("/")[1]+'_'+loopvalue[x].split("/")[2]+'">'+loopvalue[x].split("/")[2]+'</td>';
							}if((x+1)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxo'+loopvalue[x+1].split("/")[1]+'_'+loopvalue[x+1].split("/")[2]+'">'+loopvalue[x+1].split("/")[2]+'</td>';
							}if((x+2)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxo'+loopvalue[x+2].split("/")[1]+'_'+loopvalue[x+2].split("/")[2]+'">'+loopvalue[x+2].split("/")[2]+'</td>';
							}if((x+3)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td></tr>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxo'+loopvalue[x+3].split("/")[1]+'_'+loopvalue[x+3].split("/")[2]+'">'+loopvalue[x+3].split("/")[2]+'</td></tr>';
							}
						}
						if(loopvalue.length==0) {
							linehtml+='<tr><td colspan=4>'+dialingRules.outPhoneCfg+'</td></tr>';
						}
						linehtml+='<tr><td colspan=4><span style="padding-left:180px;"></span><input type=button name="ok" id=ok'+i+' value="'+sbutton.ok+'" class="button"><input type=button name="reset" value="'+sbutton.cancel+'"  class="button"></td></tr>';
						$("#linetest").empty();
						$("#linetest").html(linehtml);
						$("[name=checklineall]").click(function () {
							if($(this).attr("checked")) {
								$("[name=checkid]").attr("checked",true);
							}else {
								$("[name=checkid]").attr("checked",false);
							}
						});
						$("#ok"+i).click(function () {
							$("select").show();
							var lines='';
							var trueline='';
							$(":checkbox").each(function () {
								if($(this).attr("id").indexOf("fxo")>=0&&$(this).attr("checked")) {
									var ids=$(this).attr("id").split("_");
									lines+=ids[0].split("fxo")[1]+",";
									trueline+=ids[1]+",";
								}
							});
							lines=lines.substr(0,lines.length-1);
							trueline=trueline.substr(0,trueline.length-1);
							$("#linevalue"+i).attr("value",trueline);
							$("#linevalue"+i).attr("truevalue",lines);
							$("#layer").hide();
						});
						$("[name=reset]").click(function () {
							$("select").show();
							$("#layer").hide();
						});
						var linevalue=$("#linevalue"+i).attr("truevalue").split(",");
						var linevalue1=$("#linevalue"+i).attr("value").split(",");
						for(var j=0;j<=linevalue.length;j++) {
							$("#fxo"+linevalue[j]+"_"+linevalue1[j]).attr("checked",true);
						}
					}else {
						$(data).find('Error').each(function () {
							var code=$(this).attr('code');
							if(code==4) {
								//show_message("err",msg.cannotModify1);
								alert(msg.cannotModify1);
							}else if(code==5) {
								gotologin();
							}else {
								var strArray=$(this).attr('msg').split(" ");
								var mid=strArray[1].substr(2);
								if(mid==401) {
									//show_message("err",msg.lineNumberError);
									alert(msg.lineNumberError);
								}else {
									showerror(mid);
								}
							}
						});
					}
				});
				$("#layer").show();
			});
		});
	}else if(tmpvalue.indexOf("IP")>=0&&tmpvalue.indexOf("LIP")<0) {
		if(tmpvalue.indexOf("/S")>=0){
		listselect("str"+i,"IP/S");
		}else{
		listselect("str"+i,"IP");
		}
		var tmparray=new Array();
		var num=0;
		$.get("/xml?method=gw.config.get&id=4021&tmp="+Math.random(),function (data) {
			var fxsarray=tmpvalue.split("/")[1].split("IP")[1];
			var loopfxs=fxsarray.split(",");
			var fxstrue='';
			for(var s=0;s<loopfxs.length;s++) {
				$(data).find("value").each(function () {
					if($(this).text().split("/")[0].split("-")[1]==loopfxs[s]&&$(this).text().split("/")[2]!=="") {
						fxstrue+=$(this).text().split("/")[2]+",";
					}
					if($(this).text().split("/")[2]!=='') {
						tmparray[num]=$(this).text();
						num++;
					}
				});
			}
			fxstrue=fxstrue.substr(0,fxstrue.length-1);
			var htmls='<input type=text name="value'+i+'" id=linevalue'+i+' value="'+fxstrue+'" truevalue="'+tmpvalue.split("/")[1].split("IP")[1]+'" size="30" maxlength="30">&nbsp;&nbsp;<input type=button id=button'+i+' style="background:url(../images/r.gif);width:16px;height:15px;border:none" name="button" class="button">';
			$("#line"+i).html(htmls);
			/*if(tmpvalue.indexOf("/S")>=0) {
				var tdhtml='<select name=lineselect class="selecthtml" id="linestr'+i+'"><option value="">'+dialingRules.order2+'</option><option value="/S" selected>'+dialingRules.order1+'</option></select>';
			}else {
				var tdhtml='<select name=lineselect class="selecthtml" id="linestr'+i+'"><option value="" selected>'+dialingRules.order2+'</option><option value="/S">'+dialingRules.order1+'</option></select>';
			}*/
			//$("#linese"+i).html(tdhtml);
			$("#input"+i).blur(function () {
				var thisvalue=$(this).attr("value");
				if(thisvalue!=="") {
					if(!isNumber(thisvalue)) {
						//show_message("err",dialingRules.isnumber);
						alert(dialingRules.isnumber);
						$("#input"+i).attr("value","")
					}
				}
			});
			$("[name=value"+i+"]").blur(function () {
				var name=$(this).attr("name");
				var i=name.split("value")[1];
				var thisvalue=$(this).val();
				if(thisvalue=='') {
					$("[name=value"+i+"]").attr("truevalue",'');
				}else {
					if(thisvalue.indexOf(",")>=0) {
						var flag='';
						var z=0;
						var this_value='';
						var truevalue='';
						thisvalue=thisvalue.split(",");
						$(thisvalue).each(function () {
							for(var s=0;s<tmparray.length;s++) {
								if(tmparray[s].split("/")[2]==thisvalue[z]) {
									this_value+=tmparray[s].split("/")[2]+",";
									truevalue+=tmparray[s].split("/")[0].split("-")[1]+",";
									flag='true';
									break;
								}else if(thisvalue!="") {
									flag='false';
								}
							}
							z++;
						});
						this_value=this_value.substr(0,this_value.length-1);
						truevalue=truevalue.substr(0,truevalue.length-1);
						$("[name=value"+i+"]").attr("value",this_value);
						$("[name=value"+i+"]").attr("truevalue",truevalue);
						if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}else {
						var flag='';
						for(var s=0;s<tmparray.length;s++) {
							if(tmparray[s].split("/")[2]==thisvalue) {
								$("[name=value"+i+"]").attr("value",tmparray[s].split("/")[2]);
								$("[name=value"+i+"]").attr("truevalue",tmparray[s].split("/")[0].split("-")[1]);
								flag='true';
								break;
							}else if(thisvalue!="") {
								flag='false';
							}
						}if(flag=="false") {
							//show_message("err",dialingRules.nonumber);
							alert(dialingRules.nonumber);
							$("[name=value"+i+"]").attr("value",'');
							$("[name=value"+i+"]").attr("truevalue",'');
						}
					}
				}
			});
			$("#button"+i).click(function () {
				$("select").hide();
				var i=$(this).attr("id").split("button")[1];
				$.get("/xml?method=gw.config.get&id=4021&cookie="+cookie+"&tmp="+Math.random(),function (data) {
					if($(data).find("rsp").attr("stat")=="ok") {
						var linehtml='<tr><td class="td_title" colspan=4 id="sb3">'+ivr_menu.ip_trunk+'<span style="padding-left:50px;"></span><input type="checkbox" name="checklineall"/>&nbsp;'+sbutton.selectall+'</td></tr>';
						var loopvalue=new Array();
						var j=0;
						$(data).find('value').each(function () {
							if($(this).text().split("/")[2]!=='') {
								loopvalue[j]=$(this).text();
								j++;
							}
						});
						for(var x=0;x<loopvalue.length;x+=4) {
							if(x>=loopvalue.length) {
								linehtml+='<tr><td>&nbsp;</td>';
							}else {
								linehtml+='<tr><td><input type=checkbox name="checkid" id="ip'+loopvalue[x].split("/")[0].split("-")[1]+'_'+loopvalue[x].split("/")[2]+'">'+loopvalue[x].split("/")[2]+'</td>';
							}if((x+1)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ip'+loopvalue[x+1].split("/")[0].split("-")[1]+'_'+loopvalue[x+1].split("/")[2]+'">'+loopvalue[x+1].split("/")[2]+'</td>';
							}if((x+2)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ip'+loopvalue[x+2].split("/")[0].split("-")[1]+'_'+loopvalue[x+2].split("/")[2]+'">'+loopvalue[x+2].split("/")[2]+'</td>';
							}if((x+3)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td></tr>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="ip'+loopvalue[x+3].split("/")[0].split("-")[1]+'_'+loopvalue[x+3].split("/")[2]+'">'+loopvalue[x+3].split("/")[2]+'</td></tr>';
							}
						}
						if(loopvalue.length==0) {
							linehtml+='<tr><td colspan=4>'+dialingRules.ipPhoneCfg+'</td></tr>';
						}
						linehtml+='<tr><td colspan=4><span style="padding-left:180px;"></span><input type=button name="ok" id=ok'+i+' value="'+sbutton.ok+'" class="button"><input type=button name="reset" value="'+sbutton.cancel+'"  class="button"></td></tr>';
						$("#linetest").empty();
						$("#linetest").html(linehtml);
						$("[name=checklineall]").click(function () {
							if($(this).attr("checked")) {
								$("[name=checkid]").attr("checked",true);
							}else {
								$("[name=checkid]").attr("checked",false);
							}
						});
						$("#ok"+i).click(function () {
							$("select").show();
							var lines='';
							var trueline='';
							$(":checkbox").each(function () {
								if($(this).attr("id").indexOf("ip")>=0&&$(this).attr("checked")) {
									var ids=$(this).attr("id").split("_");
									lines+=ids[0].split("ip")[1]+",";
									trueline+=ids[1]+",";
								}
							});
							lines=lines.substr(0,lines.length-1);
							trueline=trueline.substr(0,trueline.length-1);
							$("#linevalue"+i).attr("value",trueline);
							$("#linevalue"+i).attr("truevalue",lines);
							$("#layer").hide();
						});
						$("[name=reset]").click(function () {
							$("select").show();
							$("#layer").hide();
						});
						var linevalue=$("#linevalue"+i).attr("truevalue").split(",");
						var linevalue1=$("#linevalue"+i).attr("value").split(",");
						for(var j=0;j<=linevalue.length;j++) {
							$("#ip"+linevalue[j]+"_"+linevalue1[j]).attr("checked",true);
						}
					}else {
						$(data).find('Error').each(function () {
							var code=$(this).attr('code');
							if(code==4) {
								//show_message("err",msg.cannotModify1);
								alert(msg.cannotModify1);
							}else if(code==5) {
								gotologin();
							}else {
								var strArray=$(this).attr('msg').split(" ");
								var mid=strArray[1].substr(2);
								if(mid==401) {
									//show_message("err",msg.lineNumberError);
									alert(msg.lineNumberError);
								}else {
									showerror(mid);
								}
							}
						});
					}
				});
				$("#layer").show();
			});
		});
	}else if(tmpvalue.indexOf("LIP")>=0) {
		listselect("str"+i,"LIP");
		if(tmpvalue.indexOf("/D")>=0) {
			var htmls=dialingRules.note;
		}else {
			var htmls=dialingRules.note;
		}
		$("#input"+i).blur(function () {
			var thisvalue=$(this).attr("value");
			if(thisvalue!=="") {
				if(!isNumber(thisvalue)) {
					//show_message("err",dialingRules.isnumber);
					alert(dialingRules.isnumber);
					$("#input"+i).attr("value","")
				}
			}
		});
		$("#line"+i).html(htmls);
		//$("#linese"+i).html("");
	}else {
		$("#input"+i).blur(function () {
			var thisvalue=$(this).attr("value");
			if(thisvalue!=="") {
				if(!isNumber(thisvalue)) {
					//show_message("err",dialingRules.isnumber);
					alert(dialingRules.isnumber);
					$("#input"+i).attr("value","")
				}
			}
		});
		$("#line"+i).html("");
		//$("#linese"+i).html("");
	}
}